package com.lollipop.rabbitmq.three;

import com.lollipop.rabbitmq.util.RabbitMqUtiils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @Author: Lollipop
 * @Date: 2021-10-03 17:56
 * @Description: 消息在手动应答时是否丢失？
 */
public class Task02 {
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtiils.getChannel();
        // 开启发布确认模式
        channel.confirmSelect();
        // 开启队列持久化 PS：若之前的队列没有开启持久化，需要将原队列删除或者重新创建一个持久化的队列，否则报错
        boolean durable = true;
        // channel->queue 生成队列
        channel.queueDeclare(TASK_QUEUE_NAME, durable, false, false, null);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String message = scanner.next();
            // 设置生产者发送消息为持久化消息 MessageProperties.PERSISTENT_TEXT_PLAIN
            channel.basicPublish("", TASK_QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes("utf-8"));
            System.out.println("消息发送成功:" + message);
        }
    }
}
